/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.slice.Calculate.KeXue;

import com.xcl.calculator.Dialog.*;
import com.xcl.calculator.Preference_RW;
import com.xcl.calculator.ResourceTable;
import com.xcl.calculator.XLog;
import com.xcl.calculator.slice.Calculate.KXCalAbilitySlice;
import com.xcl.calengine.ExpressionHandler;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.distributedhardware.devicemanager.DeviceInfo;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.PacMap;
import ohos.vibrator.agent.VibratorAgent;
import ohos.vibrator.bean.VibrationPattern;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.xcl.calculator.MyApplication.instance_hi;
import static com.xcl.calculator.Preference_RW.getLocalDeviceType;
import static com.xcl.calculator.slice.Calculate.KXCalAbilitySlice.ans;
import static com.xcl.calculator.slice.Calculate.KXCalAbilitySlice.que;


/**
 * The type Set provider.
 *
 * @author Xcl
 * @date 2022 /1/29
 * @package com.xcl.calculator.slice.Calculate.beta
 */
public class SetProvider extends BaseItemProvider {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "SetProvider");
    /**
     * The constant vibratorAgent.
     */
    private static final VibratorAgent vibratorAgent = new VibratorAgent();
    /**
     * The Setting list.
     */
    private final List<SetCal> settingList;
    /**
     * The Slice.
     */
    private final AbilitySlice slice;
    /**
     * The Vibrator list.
     */
    private final List<Integer> vibratorList = vibratorAgent.getVibratorIdList();
    /**
     * The Device.
     */
    private int device = 0;
    /**
     * The Jisuanan.
     */
    private int jisuanan = 0;
    /**
     * The Cpt.
     */
    private Component cpt;

    /**
     * Instantiates a new Set provider.
     *
     * @param settingList the setting list
     * @param slice       the slice
     */
    public SetProvider(List<SetCal> settingList, AbilitySlice slice) {
        this.settingList = settingList;
        this.slice = slice;
    }

    /**
     * 数据展示
     *
     * @param text1 the text 1
     */
    private static void ShowDialog(String text1) {
        try {
            if (getLocalDeviceType() == DeviceInfo.DeviceType.PHONE.value()) {
                new MyToastDialog(KXCalAbilitySlice.context1, text1, ResourceTable.Graphic_xtoast_framem, 36, 25)
                        .setDuration(134)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0, 100)
                        .show();
                XLog.info(label, "当前设备类型为手机");
            } else if (getLocalDeviceType() == DeviceInfo.DeviceType.TABLET.value()) {
                new MyToastDialog(KXCalAbilitySlice.context1, text1, ResourceTable.Graphic_xtoast_framet, 43, 37)
                        .setDuration(134)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0, 100)
                        .show();
                XLog.info(label, "当前设备类型为平板");
            } else XLog.warn(label, "暂不支持您的设备:(" + getLocalDeviceType() + ")");
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Gets count.
     *
     * @return the count
     */
    @Override
    public int getCount() {
        return this.settingList == null ? 0 : this.settingList.size();
    }

    /**
     * Gets item.
     *
     * @param i the
     * @return the item
     */
    @Override
    public Object getItem(int i) {
        if (this.settingList != null && i >= 0 && i < this.settingList.size()) return this.settingList.get(i);
        return null;
    }

    /**
     * Gets item id.
     *
     * @param i the
     * @return the item id
     */
    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * Gets component.
     *
     * @param i                  the
     * @param component          the component
     * @param componentContainer the component container
     * @return the component
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        try {
            SettingHolder holder;
            SetCal setting = this.settingList.get(i);
            if (component == null) {
                if (getLocalDeviceType() == DeviceInfo.DeviceType.PHONE.value()) {
                    this.device = ResourceTable.Layout_common_dialogm;
                    this.cpt = LayoutScatter.getInstance(this.slice).parse(ResourceTable.Layout_kxitem, null, false);
                    XLog.info(label, "当前设备类型为手机");
                } else if (getLocalDeviceType() == DeviceInfo.DeviceType.TABLET.value()) {
                    this.device = ResourceTable.Layout_common_dialogt;
                    this.cpt = LayoutScatter.getInstance(this.slice).parse(ResourceTable.Layout_kxitet, null, false);
                    XLog.info(label, "当前设备类型为平板");
                } else XLog.warn(label, "暂不支持您的设备:(" + getLocalDeviceType() + ")");
                holder = new SettingHolder(this.cpt);
                this.cpt.setTag(holder);// 将获取到的子组件信息绑定到列表项的实例中
            } else {
                this.cpt = component;
                holder = (SettingHolder) this.cpt.getTag();// 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            }
            holder.cal1.setText(setting.getAnn1());
            holder.cal1.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    XLog.info(SetProvider.label, "点击按钮:" + setting.getAnn1());
                    if (setting.getAnn1().equals("√") || setting.getAnn1().equals("rand") || setting.getAnn1().equals("re")
                            || setting.getAnn1().equals("sin") || setting.getAnn1().equals("asin") || setting.getAnn1().equals("diff")
                            || setting.getAnn1().equals("exp") || setting.getAnn1().equals("round") || setting.getAnn1().equals("prime"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn1() + "(");
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(ans.getText() + setting.getAnn1() + "(");
                    else if (setting.getAnn1().equals(",") || setting.getAnn1().equals("7") || setting.getAnn1().equals("4")
                            || setting.getAnn1().equals("1") || setting.getAnn1().equals("0") || setting.getAnn1().equals("ans")
                            || setting.getAnn1().equals("ћ") || setting.getAnn1().equals("R") || setting.getAnn1().equals("true"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn1());
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn1());
                }
            });
            holder.cal1.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    SetProvider.vibratorAgent.startOnce(SetProvider.this.vibratorList.get(0), VibrationPattern.VIBRATOR_TYPE_COMMON_LONG_PRESS1);
                    //TODO:帮助页面
                    if (setting.getAnn1().equals(",") || setting.getAnn1().equals("7") ||
                            setting.getAnn1().equals("4") || setting.getAnn1().equals("1") ||
                            setting.getAnn1().equals("0") || setting.getAnn1().equals("ћ") ||
                            setting.getAnn1().equals("ans") || setting.getAnn1().equals("R") ||
                            setting.getAnn1().equals("true")) switch (setting.getAnn1()) {
                        case ",":
                            SetProvider.ShowDialog("逗号");
                            break;
                        case "7":
                            SetProvider.ShowDialog("数字7");
                            break;
                        case "4":
                            SetProvider.ShowDialog("数字4");
                            break;
                        case "1":
                            SetProvider.ShowDialog("数字1");
                            break;
                        case "0":
                            SetProvider.ShowDialog("数字0");
                            break;
                        case "ћ":
                            SetProvider.ShowDialog("约化普朗克常数");
                            break;
                        case "ans":
                            SetProvider.ShowDialog("上次运算值");
                            break;
                        case "R":
                            SetProvider.ShowDialog("理想气体常数");
                            break;
                        case "true":
                            SetProvider.ShowDialog("真");
                            break;
                    }
                    else NiceDialog.init(KXCalAbilitySlice.context1)
                            .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                            .setSwipeToClose(true)
                            .setViewConvertListener(new ViewConvertListener() {
                                @Override
                                public void convertView(ViewHolder holder, NiceDialog dialog) {
                                    String tit0 = null, mes0 = null;
                                    switch (setting.getAnn1()) {
                                        case "√":
                                            tit0 = "平方根函数";
                                            mes0 = "√()函数返回输入参数的平方根，需要1个参数";
                                            break;
                                        case "rand":
                                            tit0 = "随机复数";
                                            mes0 = "rand()函数返回一个随机复数，不需要参数，但也可以输入一个使其缩放的参数";
                                            break;
                                        case "sin":
                                            tit0 = "正弦函数";
                                            mes0 = "sin()函数返回输入参数的正弦，需要1个参数";
                                            break;
                                        case "exp":
                                            tit0 = "exp函数";
                                            mes0 = "exp()函数返回以自然底数为底输入参数次幂的值，需要1个参数";
                                            break;
                                        case "asin":
                                            tit0 = "反正弦函数";
                                            mes0 = "asin()函数返回输入参数的反正弦，需要1个参数";
                                            break;
                                        case "round":
                                            tit0 = "四舍五入函数";
                                            mes0 = "round()函数返回输入参数四舍五入的值，至少需要1个参数，也可以输入2个参数，第1个参数为需要四舍五入的值，第2个是需要保留的小数位";
                                            break;
                                        case "re":
                                            tit0 = "实部获取函数";
                                            mes0 = "re()函数返回输入参数的实部，需要1个参数";
                                            break;
                                        case "diff":
                                            tit0 = "导函数计算";
                                            mes0 = "diff()函数返回输入参数导函数的值，至少需要2个参数，第1个参数为携带变量x的函数，第2个参数为x的值，如果你需要设置求导方向，你也可以将其输入为第3个参数";
                                            break;
                                        case "prime":
                                            tit0 = "求质数";
                                            mes0 = "prime()函数返回第输入参数个的质数，需要1个参数";
                                            break;
                                    }
                                    holder.setText(ResourceTable.Id_dl_title, tit0);
                                    holder.setText(ResourceTable.Id_dl_mess, mes0);
                                    holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                        @Override
                                        public void onClick(Component component) {
                                            dialog.hide();
                                        }
                                    });
                                }
                            })
                            .setTransparent(true)
                            .show();
                }
            });
            holder.cal2.setText(setting.getAnn2());
            holder.cal2.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    XLog.info(SetProvider.label, "点击按钮:" + setting.getAnn2());
                    if (setting.getAnn2().equals("randInt") || setting.getAnn2().equals("im") || setting.getAnn2().equals("cos")
                            || setting.getAnn2().equals("acos") || setting.getAnn2().equals("sum") || setting.getAnn2().equals("gcd")
                            || setting.getAnn2().equals("floor") || setting.getAnn2().equals("isPrime"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn2() + "(");
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn2() + "(");
                    else if (setting.getAnn2().equals("^") || setting.getAnn2().equals("∞") || setting.getAnn2().equals("8")
                            || setting.getAnn2().equals("5") || setting.getAnn2().equals("2") || setting.getAnn2().equals(".")
                            || setting.getAnn2().equals("π") || setting.getAnn2().equals("γ") || setting.getAnn2().equals("K")
                            || setting.getAnn2().equals("false")) if (SetProvider.this.jisuanan == 5) {
                        que.setText("");
                        que.setText(que.getText() + setting.getAnn2());
                        SetProvider.this.jisuanan = 1;
                    } else que.setText(que.getText() + setting.getAnn2());
                }
            });
            holder.cal2.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    SetProvider.vibratorAgent.startOnce(SetProvider.this.vibratorList.get(0), VibrationPattern.VIBRATOR_TYPE_COMMON_LONG_PRESS1);
                    //TODO:帮助页面
                    if (setting.getAnn2().equals("false") || setting.getAnn2().equals("K") ||
                            setting.getAnn2().equals("γ") || setting.getAnn2().equals("π") ||
                            setting.getAnn2().equals(".") || setting.getAnn2().equals("2") ||
                            setting.getAnn2().equals("5") || setting.getAnn2().equals("8") ||
                            setting.getAnn2().equals("∞")) switch (setting.getAnn2()) {
                        case "false":
                            SetProvider.ShowDialog("假");
                            break;
                        case "K":
                            SetProvider.ShowDialog("卡常数");
                            break;
                        case "γ":
                            SetProvider.ShowDialog("欧拉常数");
                            break;
                        case "π":
                            SetProvider.ShowDialog("圆周率");
                            break;
                        case ".":
                            SetProvider.ShowDialog("小数点");
                            break;
                        case "2":
                            SetProvider.ShowDialog("数字2");
                            break;
                        case "5":
                            SetProvider.ShowDialog("数字5");
                            break;
                        case "8":
                            SetProvider.ShowDialog("数字8");
                            break;
                        case "∞":
                            SetProvider.ShowDialog("无穷");
                            break;
                    }
                    else NiceDialog.init(KXCalAbilitySlice.context1)
                            .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                            .setSwipeToClose(true)
                            .setViewConvertListener(new ViewConvertListener() {
                                @Override
                                public void convertView(ViewHolder holder, NiceDialog dialog) {
                                    String tit0 = null, mes0 = null;
                                    switch (setting.getAnn2()) {
                                        case "randInt":
                                            tit0 = "随机整数函数";
                                            mes0 = "randInt()函数返回一个输入参数指定范围的随机整数，需要2个参数以指定随机整数的范围";
                                            break;
                                        case "im":
                                            tit0 = "虚部求解";
                                            mes0 = "im()函数返回输入参数的虚部，需要1个参数";
                                            break;
                                        case "cos":
                                            tit0 = "余弦函数";
                                            mes0 = "cos()函数返回输入参数的余弦，需要1个参数";
                                            break;
                                        case "acos":
                                            tit0 = "反余弦函数";
                                            mes0 = "acos()函数返回输入参数的反余弦，需要1个参数";
                                            break;
                                        case "sum":
                                            tit0 = "累加求和函数";
                                            mes0 = "sum()函数返回输入参数累加求和的值，需要3个参数，第1个参数为携带变量x的函数，第2个参数为累加的开始值，第3个参数为累加的结束值";
                                            break;
                                        case "gcd":
                                            tit0 = "最大公约数";
                                            mes0 = "gcd()函数返回输入的两个参数的最大公约数，需要2个参数";
                                            break;
                                        case "floor":
                                            tit0 = "向下取整函数";
                                            mes0 = "floor()函数返回输入参数向下取整的值，需要1个参数";
                                            break;
                                        case "isPrime":
                                            tit0 = "质数判断";
                                            mes0 = "isPrime()函数返回输入参数是否为质数，需要1个参数";
                                            break;
                                        case "^":
                                            tit0 = "次方运算";
                                            mes0 = "()^()次方函数返回次方计算值，需要2个参数，^前面为被次方数，^后面为次方数";
                                            break;
                                    }
                                    holder.setText(ResourceTable.Id_dl_title, tit0);
                                    holder.setText(ResourceTable.Id_dl_mess, mes0);
                                    holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                        @Override
                                        public void onClick(Component component) {
                                            dialog.hide();
                                        }
                                    });
                                }
                            })
                            .setTransparent(true)
                            .show();
                }
            });
            holder.cal3.setText(setting.getAnn3());
            holder.cal3.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    XLog.info(SetProvider.label, "点击按钮:" + setting.getAnn3());
                    if (setting.getAnn3().equals("Γ") || setting.getAnn3().equals("abs") || setting.getAnn3().equals("min")
                            || setting.getAnn3().equals("tan") || setting.getAnn3().equals("atan") || setting.getAnn3().equals("lim")
                            || setting.getAnn3().equals("lcm") || setting.getAnn3().equals("ceil") || setting.getAnn3().equals("cbrt"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn3() + "(");
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn3() + "(");
                    else if (setting.getAnn3().equals("!") || setting.getAnn3().equals("i") || setting.getAnn3().equals("9")
                            || setting.getAnn3().equals("6") || setting.getAnn3().equals("3") || setting.getAnn3().equals("%")
                            || setting.getAnn3().equals("k") || setting.getAnn3().equals("φ") || setting.getAnn3().equals("me"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn3());
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn3());
                }
            });
            holder.cal3.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    SetProvider.vibratorAgent.startOnce(SetProvider.this.vibratorList.get(0), VibrationPattern.VIBRATOR_TYPE_COMMON_LONG_PRESS1);
                    //TODO:帮助页面
                    if (setting.getAnn3().equals("!") || setting.getAnn3().equals("i") ||
                            setting.getAnn3().equals("9") || setting.getAnn3().equals("6") ||
                            setting.getAnn3().equals("3") || setting.getAnn3().equals("%") ||
                            setting.getAnn3().equals("k") || setting.getAnn3().equals("me") ||
                            setting.getAnn3().equals("φ")) switch (setting.getAnn3()) {
                        case "!":
                            SetProvider.ShowDialog("阶乘");
                            break;
                        case "i":
                            SetProvider.ShowDialog("虚数单位");
                            break;
                        case "9":
                            SetProvider.ShowDialog("数字9");
                            break;
                        case "6":
                            SetProvider.ShowDialog("数字6");
                            break;
                        case "3":
                            SetProvider.ShowDialog("数字3");
                            break;
                        case "%":
                            SetProvider.ShowDialog("百分号");
                            break;
                        case "k":
                            SetProvider.ShowDialog("玻尔兹曼常数");
                            break;
                        case "me":
                            SetProvider.ShowDialog("电子质量常数");
                            break;
                        case "φ":
                            SetProvider.ShowDialog("黄金分割常数");
                            break;
                    }
                    else NiceDialog.init(KXCalAbilitySlice.context1)
                            .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                            .setSwipeToClose(true)
                            .setViewConvertListener(new ViewConvertListener() {
                                @Override
                                public void convertView(ViewHolder holder, NiceDialog dialog) {
                                    String tit0 = null, mes0 = null;
                                    switch (setting.getAnn3()) {
                                        case "Γ":
                                            tit0 = "欧拉第二积分函数";
                                            mes0 = "Γ()函数返回输入参数的欧拉第二积分，需要1个参数";
                                            break;
                                        case "abs":
                                            tit0 = "绝对值函数";
                                            mes0 = "abs()函数返回输入参数的绝对值，但不支持复数，建议使用norm函数，需要1个参数";
                                            break;
                                        case "min":
                                            tit0 = "最小值求解";
                                            mes0 = "min()函数返回两个输入参数中最小的参数，需要2个参数";
                                            break;
                                        case "tan":
                                            tit0 = "正切函数";
                                            mes0 = "tan()函数返回输入参数的正切，需要1个参数";
                                            break;
                                        case "atan":
                                            tit0 = "反正切函数";
                                            mes0 = "atan()函数返回输入参数的反正切，需要1个参数";
                                            break;
                                        case "lim":
                                            tit0 = "求解极限";
                                            mes0 = "lim()函数返回给定函数在某一点或无穷处的极限，至少需要2个参数，你也可以输入3个参数，第1个参数为携带变量x的函数，第2个参数为变量x趋近的值，第3个为求极限的方向值";
                                            break;
                                        case "lcm":
                                            tit0 = "最小公倍数";
                                            mes0 = "lcm()函数返回输入的两个参数的最小公倍数，需要2个参数";
                                            break;
                                        case "ceil":
                                            tit0 = "向上取整函数";
                                            mes0 = "ceil()函数返回输入参数向上取整的值，需要1个参数";
                                            break;
                                        case "cbrt":
                                            tit0 = "立方根函数";
                                            mes0 = "cbrt()函数返回输入参数的立方根，需要1个参数";
                                            break;
                                    }
                                    holder.setText(ResourceTable.Id_dl_title, tit0);
                                    holder.setText(ResourceTable.Id_dl_mess, mes0);
                                    holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                        @Override
                                        public void onClick(Component component) {
                                            dialog.hide();
                                        }
                                    });
                                }
                            })
                            .setTransparent(true)
                            .show();
                }
            });
            holder.cal4.setText(setting.getAnn4());
            if (setting.getAnn4().equals("←")) holder.cal4.setTextColor(new Color(Color.rgb(160, 197, 214)));
            else
                holder.cal4.setTextColor(new Color(Color.rgb(255, 255, 255)));
            holder.cal4.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    XLog.info(SetProvider.label, "点击按钮:" + setting.getAnn4());
                    if (setting.getAnn4().equals("lg") || setting.getAnn4().equals("max") || setting.getAnn4().equals("arg")
                            || setting.getAnn4().equals("norm") || setting.getAnn4().equals("fzero") || setting.getAnn4().equals("perm")
                            || setting.getAnn4().equals("sign") || setting.getAnn4().equals("root"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn4() + "(");
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn4() + "(");
                    else if (setting.getAnn4().equals("(") || setting.getAnn4().equals("x") || setting.getAnn4().equals("•")
                            || setting.getAnn4().equals("+") || setting.getAnn4().equals("e") || setting.getAnn4().equals("F")
                            || setting.getAnn4().equals("c") || setting.getAnn4().equals("G") || setting.getAnn4().equals("mn"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn4());
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn4());
                    else if (setting.getAnn4().equals("←")) {
                        //撤回
                        String current = que.getText();
                        if (SetProvider.this.jisuanan == 5) que.setText("");
                        else if (current.length() > 0) if (current.length() >= 8 && (current.endsWith("randInt(") ||
                                current.endsWith("isPrime(")))
                            que.setText(current.substring(0, current.length() - 8));
                        else if (current.length() >= 6 && (current.endsWith("round(") ||
                                current.endsWith("prime(") ||
                                current.endsWith("floor(") ||
                                current.endsWith("fzero(") ||
                                current.endsWith("integ(")))
                            que.setText(current.substring(0, current.length() - 6));
                        else if (current.length() >= 5 && (current.endsWith("asin(") ||
                                current.endsWith("diff(") ||
                                current.endsWith("acos(") ||
                                current.endsWith("atan(") ||
                                current.endsWith("ceil(") ||
                                current.endsWith("cbrt(") ||
                                current.endsWith("norm(") ||
                                current.endsWith("sign(") ||
                                current.endsWith("root(") ||
                                current.endsWith("perm(") ||
                                current.endsWith("eval(") ||
                                current.endsWith("comb(") ||
                                current.endsWith("false") ||
                                current.endsWith("conj(") ||
                                current.endsWith("remn("))) que.setText(current.substring(0, current.length() - 5));
                        else if (current.length() >= 4 && (current.endsWith("sin(") ||
                                current.endsWith("exp(") ||
                                current.endsWith("cos(") ||
                                current.endsWith("gcd(") ||
                                current.endsWith("sum(") ||
                                current.endsWith("tan(") ||
                                current.endsWith("lcm(") ||
                                current.endsWith("abs(") ||
                                current.endsWith("min(") ||
                                current.endsWith("lim(") ||
                                current.endsWith("max(") ||
                                current.endsWith("arg(") ||
                                current.endsWith("true") ||
                                current.endsWith("log("))) que.setText(current.substring(0, current.length() - 4));
                        else if (current.length() >= 3 && (current.endsWith("re(") ||
                                current.endsWith("im(") ||
                                current.endsWith("lg(") ||
                                current.endsWith("ln(") ||
                                current.endsWith("ans"))) que.setText(current.substring(0, current.length() - 3));
                        else if (current.length() >= 2 && (current.endsWith("√(") ||
                                current.endsWith("mn") ||
                                current.endsWith("me") ||
                                current.endsWith("Γ("))) que.setText(current.substring(0, current.length() - 2));
                        else
                            que.setText(current.substring(0, current.length() - 1));
                    }
                }
            });
            holder.cal4.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    SetProvider.vibratorAgent.startOnce(SetProvider.this.vibratorList.get(0), VibrationPattern.VIBRATOR_TYPE_COMMON_LONG_PRESS1);
                    //TODO:帮助页面
                    if (setting.getAnn4().equals("←") || setting.getAnn4().equals("(") ||
                            setting.getAnn4().equals("x") || setting.getAnn4().equals("•") ||
                            setting.getAnn4().equals("+") || setting.getAnn4().equals("e") ||
                            setting.getAnn4().equals("F") || setting.getAnn4().equals("c") ||
                            setting.getAnn4().equals("G") || setting.getAnn4().equals("mn"))
                        switch (setting.getAnn4()) {
                            case "←":
                                SetProvider.ShowDialog("回退一格");
                                break;
                            case "(":
                                SetProvider.ShowDialog("左半个括号");
                                break;
                            case "x":
                                SetProvider.ShowDialog("英文X");
                                break;
                            case "•":
                                SetProvider.ShowDialog("乘号");
                                break;
                            case "+":
                                SetProvider.ShowDialog("加号");
                                break;
                            case "e":
                                SetProvider.ShowDialog("自然常数");
                                break;
                            case "F":
                                SetProvider.ShowDialog("法拉第常数");
                                break;
                            case "c":
                                SetProvider.ShowDialog("光速常数");
                                break;
                            case "G":
                                SetProvider.ShowDialog("万有引力常数");
                                break;
                            case "mn":
                                SetProvider.ShowDialog("质子质量常数");
                                break;

                        }
                    else NiceDialog.init(KXCalAbilitySlice.context1)
                            .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                            .setSwipeToClose(true)
                            .setViewConvertListener(new ViewConvertListener() {
                                @Override
                                public void convertView(ViewHolder holder, NiceDialog dialog) {
                                    String tit0 = null, mes0 = null;
                                    switch (setting.getAnn4()) {
                                        case "lg":
                                            tit0 = "对数函数";
                                            mes0 = "lg()函数返回输入参数以10为底输入参数的对数，需要1个参数";
                                            break;
                                        case "max":
                                            tit0 = "最大值求解";
                                            mes0 = "max()函数返回两个输入参数中最大的参数，需要2个参数";
                                            break;
                                        case "arg":
                                            tit0 = "辐角计算";
                                            mes0 = "arg()函数返回输入参数的辐角，需要1个参数";
                                            break;
                                        case "norm":
                                            tit0 = "模长计算";
                                            mes0 = "norm()函数返回输入参数的模长，需要1个参数";
                                            break;
                                        case "fzero":
                                            tit0 = "解方程";
                                            mes0 = "fzero()函数返回给定函数值为0时变量x的值，至少需要1个参数，第1个参数为携带变量x的函数，你可以尝试估算函数值为0时x的值然后输入为第2个参数作为寻找函数值为0时的初始值，这样可以增加运算速度和成功率，计算失败或计算时间过长请减少数据输入量";
                                            break;
                                        case "perm":
                                            tit0 = "排列数量计算";
                                            mes0 = "perm()函数返回输入参数的排列数量，需要2个参数，第1个参数为互异元素的数量，第2个参数为取出的数量";
                                            break;
                                        case "sign":
                                            tit0 = "正负性判断";
                                            mes0 = "sign()函数返回输入参数的正负性，若为正数返回1，若为0返回0，若为负数返回-1，需要1个参数";
                                            break;
                                        case "root":
                                            tit0 = "开跟函数";
                                            mes0 = "root()函数返回输入参数的根，需要2个参数，第1个参数为所开方的数，第2个是开方的次数";
                                            break;
                                    }
                                    holder.setText(ResourceTable.Id_dl_title, tit0);
                                    holder.setText(ResourceTable.Id_dl_mess, mes0);
                                    holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                        @Override
                                        public void onClick(Component component) {
                                            dialog.hide();
                                        }
                                    });
                                }
                            })
                            .setTransparent(true)
                            .show();
                }
            });
            holder.cal5.setText(setting.getAnn5());
            if (setting.getAnn5().equals("=")) holder.cal5.setTextColor(new Color(Color.rgb(31, 90, 141)));
            else if (setting.getAnn5().equals("AC")) holder.cal5.setTextColor(new Color(Color.rgb(229, 63, 63)));
            else
                holder.cal5.setTextColor(new Color(Color.rgb(255, 255, 255)));
            holder.cal5.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    XLog.info(SetProvider.label, "点击按钮:" + setting.getAnn5());
                    if (setting.getAnn5().equals("ln") || setting.getAnn5().equals("log") || setting.getAnn5().equals("eval")
                            || setting.getAnn5().equals("conj") || setting.getAnn5().equals("integ") || setting.getAnn5().equals("comb")
                            || setting.getAnn5().equals("remn")) if (SetProvider.this.jisuanan == 5) {
                        que.setText("");
                        que.setText(que.getText() + setting.getAnn5() + "(");
                        SetProvider.this.jisuanan = 1;
                    } else que.setText(que.getText() + setting.getAnn5() + "(");
                    else if (setting.getAnn5().equals(")") || setting.getAnn5().equals("°") || setting.getAnn5().equals("/")
                            || setting.getAnn5().equals("-") || setting.getAnn5().equals("h") || setting.getAnn5().equals("N")
                            || setting.getAnn5().equals("Φ") || setting.getAnn5().equals("mp"))
                        if (SetProvider.this.jisuanan == 5) {
                            que.setText("");
                            que.setText(que.getText() + setting.getAnn5());
                            SetProvider.this.jisuanan = 1;
                        } else que.setText(que.getText() + setting.getAnn5());
                    else if (setting.getAnn5().equals("=")) {
                        //计算
                        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
                        Matcher m = p.matcher(que.getText());
                        if (m.find()) {
                            que.setText("");
                            ans.setText("输入数据有误!");
                        } else {
                            String[] value = ExpressionHandler.calculation(que.getText());
                            SetProvider.this.jisuanan = 5;
                            ans.setText(value[0]);
                            //TODO:自定义埋点开始，在工程合适位置插入埋点代码
                            PacMap pacMap = new PacMap();
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                            pacMap.putString("user_uuid", "[" + Preference_RW.uuid.toString() + "]");
                            pacMap.putString("start_num", "[" + que.getText() + "]");
                            pacMap.putString("end_num", "[=" + ans.getText() + "]");
                            pacMap.putString("se_time", "[" + df.format(new Date()) + "]");
                            instance_hi.onEvent("dgnkxjs", pacMap);
                            //TODO:埋点结束
                        }
                    } else //清空
                        if (setting.getAnn5().equals("AC")) {
                            que.setText("");
                            ans.setText("");
                        } else //帮助
                            if (setting.getAnn5().equals("Help")) NiceDialog.init(KXCalAbilitySlice.context1)
                                    .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                                    .setSwipeToClose(true)
                                    .setViewConvertListener(new ViewConvertListener() {
                                        @Override
                                        public void convertView(ViewHolder holder, NiceDialog dialog) {
                                            holder.setText(ResourceTable.Id_dl_title, "帮助");
                                            holder.setText(ResourceTable.Id_dl_mess, "1.每个按钮长按可以查看每个按钮功能的使用说明\n" +
                                                    "2.上下滑动可以使用全部功能\n" +
                                                    "3.输入数据或结果数据过长时请上下滑动查看全部");
                                            holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                                @Override
                                                public void onClick(Component component) {
                                                    dialog.hide();
                                                }
                                            });
                                        }
                                    })
                                    .setTransparent(true)
                                    .show();
                }
            });
            holder.cal5.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    SetProvider.vibratorAgent.startOnce(SetProvider.this.vibratorList.get(0), VibrationPattern.VIBRATOR_TYPE_COMMON_LONG_PRESS1);
                    //TODO:帮助页面
                    if (setting.getAnn5().equals("/") || setting.getAnn5().equals(")") ||
                            setting.getAnn5().equals("-") || setting.getAnn5().equals("h") ||
                            setting.getAnn5().equals("N") || setting.getAnn5().equals("Φ") ||
                            setting.getAnn5().equals("mp") || setting.getAnn5().equals("AC") ||
                            setting.getAnn5().equals("=") || setting.getAnn5().equals("Help") ||
                            setting.getAnn5().equals("°")) switch (setting.getAnn5()) {
                        case "/":
                            SetProvider.ShowDialog("除号");
                            break;
                        case ")":
                            SetProvider.ShowDialog("右半个括号");
                            break;
                        case "-":
                            SetProvider.ShowDialog("减号");
                            break;
                        case "h":
                            SetProvider.ShowDialog("普朗克常数");
                            break;
                        case "N":
                            SetProvider.ShowDialog("阿伏伽德罗常数");
                            break;
                        case "Φ":
                            SetProvider.ShowDialog("磁通量子常数");
                            break;
                        case "mp":
                            SetProvider.ShowDialog("中子质量常数");
                            break;
                        case "=":
                            SetProvider.ShowDialog("点击计算");
                            break;
                        case "AC":
                            SetProvider.ShowDialog("点击清空");
                            break;
                        case "Help":
                            SetProvider.ShowDialog("点击查看帮助");
                            break;
                        case "°":
                            SetProvider.ShowDialog("度数(角度)");
                            break;
                    }
                    else NiceDialog.init(KXCalAbilitySlice.context1)
                            .setLayoutId(SetProvider.this.device, ConstandCodes.CONFRIMTYPE)
                            .setSwipeToClose(true)
                            .setViewConvertListener(new ViewConvertListener() {
                                @Override
                                public void convertView(ViewHolder holder, NiceDialog dialog) {
                                    String tit0 = null, mes0 = null;
                                    switch (setting.getAnn5()) {
                                        case "ln":
                                            tit0 = "对数计算";
                                            mes0 = "ln()函数返回输入参数以自然底数为底输入参数的对数，需要1个参数";
                                            break;
                                        case "log":
                                            tit0 = "对数计算";
                                            mes0 = "log()函数返回输入参数的对数，需要2个参数，第1个参数为底数，第2个参数为真数";
                                            break;
                                        case "eval":
                                            tit0 = "函数值计算";
                                            mes0 = "eval()函数返回给定函数变量x为某个值时函数的值，需要2个参数，第1个参数为携带变量x的函数，第2个参数为变量x的值";
                                            break;
                                        case "conj":
                                            tit0 = "共轭计算";
                                            mes0 = "conj()函数返回输入参数的共轭，需要1个参数";
                                            break;
                                        case "integ":
                                            tit0 = "定积分计算";
                                            mes0 = "integ()函数返回输入参数的定积分，需要3个参数，第1个参数为携带变量x的函数，第2个参数为积分的下限，第3个参数为积分的上限";
                                            break;
                                        case "comb":
                                            tit0 = "组合数计算";
                                            mes0 = "comb()函数返回输入参数的组合数量，需要2个参数，第1个参数为互异元素的数量，第2个参数为取出的数量";
                                            break;
                                        case "remn":
                                            tit0 = "余数计算";
                                            mes0 = "remn()函数返回输入参数的余数，需要2个参数，第1个参数为被除数，第2个参数为余数";
                                            break;
                                    }
                                    holder.setText(ResourceTable.Id_dl_title, tit0);
                                    holder.setText(ResourceTable.Id_dl_mess, mes0);
                                    holder.setOnClickListener(ResourceTable.Id_dl_sure, new Component.ClickedListener() {
                                        @Override
                                        public void onClick(Component component) {
                                            dialog.hide();
                                        }
                                    });
                                }
                            })
                            .setTransparent(true)
                            .show();
                }
            });
        } catch (Exception e) {
            XLog.pd_pd = Preference_RW.ff7_r();
            XLog.error(label, e.getMessage());
        }
        return this.cpt;
    }

    /**
     * The type Setting holder.
     */
    public class SettingHolder {
        /**
         * The Cal 1.
         */
        Button cal1,
        /**
         * The Cal 2.
         */
        cal2,
        /**
         * The Cal 3.
         */
        cal3,
        /**
         * The Cal 4.
         */
        cal4,
        /**
         * The Cal 5.
         */
        cal5;

        /**
         * Instantiates a new Setting holder.
         *
         * @param component the component
         */
        SettingHolder(Component component) {
            this.cal1 = (Button) component.findComponentById(ResourceTable.Id_kx_0_button);
            this.cal2 = (Button) component.findComponentById(ResourceTable.Id_kx_1_button);
            this.cal3 = (Button) component.findComponentById(ResourceTable.Id_kx_2_button);
            this.cal4 = (Button) component.findComponentById(ResourceTable.Id_kx_3_button);
            this.cal5 = (Button) component.findComponentById(ResourceTable.Id_kx_4_button);
        }
    }
}
